/***
 * @file: 请求拦截
 * @author: xiaoyu
 * @Date: 2021-07-07 11:39:24
 */
import Axios from 'axios'
import { message } from 'ant-design-vue'

const envConfig = import.meta.env

const baseUrl = envConfig.VITE_BASE_API as string

const axios = Axios.create({
  baseURL: baseUrl,
  timeout: 30000
})

axios.interceptors.request.use(
  (request) => {
    return request
  },
  (error) => {
    message.error('请求失败，请重新请求！')
  }
)

axios.interceptors.response.use(
  (response) => {
    if (response && response.status == 200) {
      return response.data
    } else {
      message.error('请求数据发生异常，请重新请求！')
    }
  },
  (error) => {
    if (error.response && error.response.data) {
      const code = error.response.status
      const msg = error.response.data.message
      message.error(`Code: ${code}, Message: ${msg}`)
      console.error(`[Axios Error]`, error.response)
    } else {
      message.error(`${error}`)
    }
    return Promise.reject(error)
  }
)

export default axios
